<?php
declare (strict_types=1);

namespace app\exam\model;

use app\common\model\BaseModel;
use app\common\model\Attach;
use think\model\concern\SoftDelete;

/**
 * 考试版块模型
 */
class Module extends BaseModel
{
    use softDelete;
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'exam_module';

    /**
     * 定义字段类型
     * @var [type]
     */
    protected $type = [
        'update_time'   => 'intger',
        'delete_time'   => 'intger'
    ];

    /**
     * 全局查询范围
     */
    protected $globalScope = ['mhm', 'status'];

    /**
     * 限制机构条件
     * @Author 亓官雨树<lucky.max@foxmail.com>
     * @Date   2021-01-21
     * @param  [type]                      $query [description]
     * @return [type]                             [description]
     */
    public function scopeMhm($query)
    {
        // 机构
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 限制禁用条件
     * @Author 亓官雨树<lucky.max@foxmail.com>
     * @Date   2021-02-04
     * @param  [type]                      $query [description]
     * @return [type]                             [description]
     */
    public function scopeStatus($query)
    {
        // 禁用状态
        $query->where('status', 1);
    }

    /**
     * 封面url
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-02-04
     * @param  string $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    protected function getCoverUrlAttr($value='', $data)
    {
        if ( $data['cover'] ) {
            $attach = app(Attach::class)->findOrEmpty($data['cover'])->append(['fileurl']);
            return $attach->isEmpty() ? '' : $attach->fileurl;
        }
        return '';
    }

}
